/*
Simple Forum Auto Poster.
Copyright (C) 2011 Gianni Van Hoecke.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package gui.result;

import domain.DomainController;
import java.awt.Dimension;
import javax.swing.WindowConstants;
import javax.swing.table.TableModel;

/**
 *
 * @author Gianni Van Hoecke <gianni.vh@gmail.com>
 */
public class PostResultContainerFrame extends javax.swing.JFrame
{
    private TableModel model;
    private String[] headers;
    private Object[][] data;

    /** Creates new form PostResultContainerFrame */
    public PostResultContainerFrame()
    {
        super("Posting errors");
        initComponents();
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        Dimension d = new Dimension(700, 400);
        this.setPreferredSize(d);
        this.setSize(d);
        this.setMinimumSize(d);

        this.tblErrors.setDefaultRenderer(Object.class, new MyCellRenderer());
        this.tblErrors.setAutoCreateRowSorter(true);
        this.resultActionPanel1.setParent(this);
        this.headers = new String[]{"ID", "Forum name", "Forum type", "Login", "Post"};
        this.data = DomainController.getInstance().getErrorCollection().getErrorsAsArray();
        this.model = new MyTableModel(this.data, this.headers);
        this.tblErrors.setModel(this.model);
        this.tblErrors.getTableHeader().setReorderingAllowed(false);
        this.setTableColumnsWidth();

        this.resultActionPanel1.setButtonsEnabled(this.data.length > 0);
    }

    private void setTableColumnsWidth()
    {
        this.tblErrors.getColumnModel().getColumn(0).setPreferredWidth(50);
        this.tblErrors.getColumnModel().getColumn(1).setPreferredWidth(350);
        this.tblErrors.getColumnModel().getColumn(2).setPreferredWidth(100);
        this.tblErrors.getColumnModel().getColumn(3).setPreferredWidth(100);
        this.tblErrors.getColumnModel().getColumn(4).setPreferredWidth(100);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblErrors = new javax.swing.JTable();
        resultActionPanel1 = new gui.result.ResultActionPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tblErrors.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblErrors.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(tblErrors);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
        getContentPane().add(resultActionPanel1, java.awt.BorderLayout.PAGE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private gui.result.ResultActionPanel resultActionPanel1;
    private javax.swing.JTable tblErrors;
    // End of variables declaration//GEN-END:variables

    public int getSelectedError()
    {
        try
        {
            return ((Integer)this.tblErrors.getValueAt(this.tblErrors.getSelectedRow(), 0)).intValue();
        }
        catch(Exception e)
        {
            return 0;
        }
    }

    public void clearList()
    {
        this.model = new MyTableModel(null, this.headers);
        this.tblErrors.setModel(this.model);
        this.resultActionPanel1.setButtonsEnabled(false);
        this.setTableColumnsWidth();
        DomainController.getInstance().clearErrorList();
    }
}